<?php
/**
 * author      : Administrator
 * createTime  : 2016/7/26 9:04
 */
namespace Home\Controller;

use Think\Controller;

class UserController extends Controller
{
    /* public function _initialize()
     {

     }*/

    /**
     * 登录
     */
    public function Login()
    {
        //1 校验验证码
        if (!$this->checkVerify()) {
            //验证码错误
            $this->error('验证码错误');
        }
        //2匹配数据库是否存在
        $data['user_name'] = I('username');
        $data['user_pwd'] = md5(I('password'));
        $user_model = D('User');
        //判断是否登录成功
        if ($user_model->getUserByUserName($data)) {
            //成功，添加session
            session('blog_user_name', $data['user_name']);  //设置session
            echo 'login sucess';
        } else {
            echo 'login false';
        }
    }

    public function listLogin()
    {
        $this->display('login');
    }

    /**
     * 注册
     */
    public function register()
    {
        //接收数据
        $data['user_name'] = I('username');
        $data['user_pwd'] = md5(I('password'));
        $data['user_sex'] = I('sex');
        $data['user_email'] = I('email');
        $data['user_address'] = I('address');
        $data['user_description'] = I('description');
        $data['user_register_time'] = time();

        //获取url并存到数据库
        $data['user_image_url'] = $this->upload();
        //添加到数据库
        $user_model = D('User');
        if ($user_model->add($data)) {
            $this->success('注册成功', 'listLogin');
        } else {
            $this->error('注册失败');
        }
    }

    /**
     * 展示注册列表
     */
    public function listRegister()
    {
        $this->display('register');
    }

    /**
     * 验证码
     */
    public function verify()
    {
        $config = array('fontSize' => 50, // 验证码字体大小
            'length' => 4,     // 验证码位数
            'useNoise' => false, // 关闭验证码杂点
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    /**
     * 上传文件
     */
    private function upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;//设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './';
        $upload->savePath = "./Public/Uploads/"; // 设置附件上传目录
        //上传文件
        $info = $upload->upload();
        if (!$info) {
            //上传错误信息
            $this->error($upload->getError());
        } else {//上传成功
            foreach ($info as $file) {
                $url = $file['savepath'] . $file['savename'];
            }
            return $url;
        }
    }

    /**
     * 校验验证码
     */
    private function checkVerify()
    {
        $Verify = new \Think\Verify();
        if (!$Verify->check(I('verify'))) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 判断是否登录
     */
    private function isLogin()
    {
        if (session('?blog_user_name')) {
            return true;
        } else {
            return false;
        }
    }
}